package api

import (
	"context"
	"github.com/cloudwego/hertz/pkg/app"
	"monthly/biz/handler/request"
	pg "monthly/kitex_gen/monthly/passenger"
	"monthly/utils"
)

var (
	Client = utils.GetPassengerClient()
)

func SendSms(ctx context.Context, c *app.RequestContext) {
	var req request.SendSmsReq
	if err := c.Bind(&req); err != nil {
		c.JSON(200, map[string]interface{}{
			"code": 400,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	sms, _ := Client.SendSms(ctx, &pg.SendSmsReq{
		Mobile:      req.Mobile,
		SendSmsCode: req.SendSmsCode,
	})
	c.JSON(200, sms)
}

func Register(ctx context.Context, c *app.RequestContext) {
	var req request.RegisterReq
	if err := c.Bind(&req); err != nil {
		c.JSON(200, map[string]interface{}{
			"code": 400,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	register, _ := Client.Register(ctx, &pg.RegisterReq{
		Mobile:      req.Mobile,
		SendSmsCode: req.SendSmsCode,
	})
	c.JSON(200, register)
}

func Login(ctx context.Context, c *app.RequestContext) {
	var req request.LoginReq
	if err := c.Bind(&req); err != nil {
		c.JSON(200, map[string]interface{}{
			"code": 400,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	login, _ := Client.Login(ctx, &pg.LoginReq{
		Mobile:      req.Mobile,
		SendSmsCode: req.SendSmsCode,
	})
	if login.Code != 200 {
		c.JSON(200, login)
		return
	}
	c.JSON(200, map[string]interface{}{
		"code": 200,
		"msg":  "登录成功",
	})
}

func UploadPassenger(ctx context.Context, c *app.RequestContext) {
	var req request.UploadPassengerReq
	if err := c.Bind(&req); err != nil {
		c.JSON(200, map[string]interface{}{
			"code": 400,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	passenger, _ := Client.UploadPassenger(ctx, &pg.UploadPassengerReq{
		Pid:          int16(req.Id),
		Mobile:       req.Mobile,
		HandlerImage: int16(req.HandlerImage),
		Name:         req.Name,
		Sex:          req.Sex,
		Age:          int16(req.Age),
	})
	c.JSON(200, passenger)
}

func SartRating(ctx context.Context, c *app.RequestContext) {
	var req request.SartRatingReq
	if err := c.Bind(&req); err != nil {
		c.JSON(200, map[string]interface{}{
			"code": 400,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	rating, _ := Client.SartRating(ctx, &pg.SartRatingReq{
		Pid:        int16(req.Id),
		StarRating: req.StarRating,
	})
	c.JSON(200, rating)
}

func QueryPTrip(ctx context.Context, c *app.RequestContext) {
	var req request.QueryPTripReq
	if err := c.Bind(&req); err != nil {
		c.JSON(200, map[string]interface{}{
			"code": 400,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	trip, _ := Client.QueryPTrip(ctx, &pg.QueryPTripReq{
		PId: int16(req.Id),
	})

	c.JSON(200, trip)
}
